package com.yonglian.web.controller.system;

import java.util.List;

import com.yonglian.system.domain.CmsProjectBase;
import com.yonglian.system.service.ICmsProjectBaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yonglian.common.annotation.Log;
import com.yonglian.common.enums.BusinessType;
import com.yonglian.system.domain.CmsActivity;
import com.yonglian.system.service.ICmsActivityService;
import com.yonglian.common.core.controller.BaseController;
import com.yonglian.common.core.domain.AjaxResult;
import com.yonglian.common.utils.poi.ExcelUtil;
import com.yonglian.common.core.page.TableDataInfo;

/**
 * 活动Controller
 *
 * @author gyok
 * @date 2024-05-28
 */
@Controller
@RequestMapping("/system/activity")
public class CmsActivityController extends BaseController
{
    private String prefix = "system/activity";

    @Autowired
    private ICmsActivityService cmsActivityService;

    @Autowired
    private ICmsProjectBaseService cmsProjectBaseService;

    @RequiresPermissions("system:activity:view")
    @GetMapping()
    public String activity()
    {
        return prefix + "/activity";
    }

    /**
     * 查询活动列表
     */
    @RequiresPermissions("system:activity:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsActivity cmsActivity)
    {
        startPage();
        List<CmsActivity> list = cmsActivityService.selectCmsActivityList(cmsActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @RequiresPermissions("system:activity:export")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsActivity cmsActivity)
    {
        List<CmsActivity> list = cmsActivityService.selectCmsActivityList(cmsActivity);
        ExcelUtil<CmsActivity> util = new ExcelUtil<CmsActivity>(CmsActivity.class);
        return util.exportExcel(list, "活动数据");
    }

    /**
     * 新增活动
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<CmsProjectBase> projectList = cmsProjectBaseService.selectCmsProjectBaseList(null);
        mmap.put("projectList", projectList);
        return prefix + "/add";
    }

    /**
     * 新增保存活动
     */
    @RequiresPermissions("system:activity:add")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsActivity cmsActivity)
    {
        return toAjax(cmsActivityService.insertCmsActivity(cmsActivity));
    }

    /**
     * 修改活动
     */
    @RequiresPermissions("system:activity:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsActivity cmsActivity = cmsActivityService.selectCmsActivityById(id);
        List<CmsProjectBase> projectList = cmsProjectBaseService.selectCmsProjectBaseList(null);
        mmap.put("cmsActivity", cmsActivity);
        mmap.put("projectList", projectList);
        return prefix + "/edit";
    }

    /**
     * 修改保存活动
     */
    @RequiresPermissions("system:activity:edit")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsActivity cmsActivity)
    {
        return toAjax(cmsActivityService.updateCmsActivity(cmsActivity));
    }

    /**
     * 删除活动
     */
    @RequiresPermissions("system:activity:remove")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cmsActivityService.deleteCmsActivityByIds(ids));
    }
}
